package com.moonshineproject.plugin.recentlyopened
{
	import com.moonshineproject.events.FilePluginEvent;
	import com.moonshineproject.events.OpenFileEvent;
	import com.moonshineproject.plugin.PluginBase;
	import com.moonshineproject.plugin.project.event.ProjectEvent;
	
	import flash.filesystem.File;
	import flash.net.SharedObject;
	
	import mx.events.CollectionEvent;

	public class RecentlyOpenedPlugin extends PluginBase
	{
		override public function get name():String			{ return "Recently Opened Plugin"; }
		override public function get author():String		{ return "Moonshine Project Team"; }
		override public function get description():String	{ return "Stores the last opened file paths."; }
		
		private var cookie:SharedObject;
		
		override public function activate():void
		{
			super.activate();
			
			cookie = SharedObject.getLocal("moonshine-ide-local");
			
			if (model.recentlyOpenedFiles.length == 0)
			{
				restoreFromCookie();
			}
			
			dispatcher.addEventListener(ProjectEvent.ADD_PROJECT, handleAddProject);
			// Give other plugins a chance to cancel the event
			dispatcher.addEventListener(FilePluginEvent.EVENT_FILE_OPEN, handleOpenFile, false, -100);
		}
		
		private function restoreFromCookie():void
		{
			// Uncomment & run to delete cookie
			//delete cookie.data.recentFiles;
			//delete cookie.data.recentProjects;
			
			// Load & unserialize recent items
			if (cookie.data.hasOwnProperty('recentFiles'))
			{
				var recentFiles:Array = cookie.data.recentFiles;
				var recent:Array = [];
				var f:File; 
				for each (var path:String in recentFiles)
				{
					f = new File(path);
					if (f.exists)
						recent.push(f);
				}
				model.recentlyOpenedFiles.source = recent;
			}
			if (cookie.data.hasOwnProperty('recentProjects'))
			{
				var recentProjects:Array = cookie.data.recentProjects;
				recent = [];
				for each (path in recentProjects)
				{
					f = new File(path);
					if (f.exists)
						recent.push(f);
				}
				model.recentlyOpenedProjects.source = recent;	
			}
		}
		
		private function handleAddProject(event:ProjectEvent):void
		{
			// Find & remove project if already present
			var f:File = (event.project.projectFile) ? event.project.projectFile : event.project.folder;
			var toRemove:int = -1;
			for each (var file:File in model.recentlyOpenedProjects)
			{
				if (file.nativePath == f.nativePath)
				{
					toRemove = model.recentlyOpenedProjects.getItemIndex(file);
					break;
				}
			}
			if (toRemove != -1) model.recentlyOpenedProjects.removeItemAt(toRemove);
			
			model.recentlyOpenedProjects.addItemAt(f, 0);
			
			save(model.recentlyOpenedProjects.source, 'recentProjects');
		}
		
		private function handleOpenFile(event:FilePluginEvent):void
		{
			if (event.isDefaultPrevented()) return;

			// File might have been removed
			var f:File = event.file;
			if (!f || !f.exists) return;			
			
			// Find item & remove it if already present (path-based, since it's two different File objects)
			var toRemove:int = -1;
			for each (var file:File in model.recentlyOpenedFiles)
			{
				if (file.nativePath == f.nativePath)
				{
					toRemove = model.recentlyOpenedFiles.getItemIndex(file);
					break;
				}
			}
			if (toRemove != -1) model.recentlyOpenedFiles.removeItemAt(toRemove);
			
			
			model.recentlyOpenedFiles.addItemAt(f, 0);
			
			// Persist to disk
			save(model.recentlyOpenedFiles.source, 'recentFiles');
		}
		
		private function save(recent:Array, key:String):void
		{
			// Only save the ten latest files
			if (recent.length > 10)
			{
				recent = recent.slice(0, 10);
			}
			// Serialize down to paths
			var toSave:Array = [];
			for each (var f:File in recent)
			{
				toSave.push(f.nativePath);
			}
			
			// Add to LocalObject
			cookie.data[key] = toSave;
			cookie.flush();	
		}
	}
}